package com.njkwt.business.system.controller;

import com.njkwt.business.system.dto.SysMessageDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.dto.ResponseDto;
import com.njkwt.business.system.service.SysMessageService;
import com.njkwt.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* @ClassName SysMessageController
* @Description
* @Author ydj (171799383@qq.com)
* @Date  2020-8-18 14:44:36
* @Version V1.0
**/

@RestController
@RequestMapping("/api/business/system/sysMessage")
public class SysMessageController {

    private static final Logger LOG = LoggerFactory.getLogger(SysMessageController.class);
    public static final String BUSINESS_NAME = "系统消息表";

    @Resource
    private SysMessageService sysMessageService;

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        sysMessageService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody SysMessageDto sysMessageDto) {
        // 保存校验
        ValidatorUtil.require(sysMessageDto.getId(), "");
        ValidatorUtil.length(sysMessageDto.getId(), "", 1, 50);
        ValidatorUtil.length(sysMessageDto.getTypename(), "", 1, 20);
        ValidatorUtil.length(sysMessageDto.getTypeid(), "", 1, 50);
        ValidatorUtil.length(sysMessageDto.getFromid(), "", 1, 50);
        ValidatorUtil.require(sysMessageDto.getToid(), "");
        ValidatorUtil.length(sysMessageDto.getToid(), "", 1, 50);
        ValidatorUtil.length(sysMessageDto.getFromname(), "", 1, 50);
        ValidatorUtil.length(sysMessageDto.getToname(), "", 1, 50);
        ValidatorUtil.require(sysMessageDto.getFromstatus(), "-1:已删除；0:默认");
        ValidatorUtil.require(sysMessageDto.getTostatus(), "-1:已删除；0:默认未读；1：已读");
        ValidatorUtil.length(sysMessageDto.getHref(), "点击消息跳转的页面等", 1, 200);
        ValidatorUtil.length(sysMessageDto.getTitle(), "", 1, 200);
        ValidatorUtil.length(sysMessageDto.getContent(), "", 1, 1000);
        ValidatorUtil.require(sysMessageDto.getCreatetime(), "");
        ValidatorUtil.length(sysMessageDto.getCreateid(), "", 1, 50);

        ResponseDto responseDto = new ResponseDto();
        sysMessageService.save(sysMessageDto);
        responseDto.setContent(sysMessageDto);
        return responseDto;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        sysMessageService.delete(id);
        return responseDto;
    }
}
